package com.elong.qadev.dao.impl;

import java.util.ArrayList;
import java.util.Iterator;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.elong.qadev.dao.HttpCaseDaoI;
import com.elong.qadev.entity.DBLink;
import com.elong.qadev.entity.DBSql;
import com.elong.qadev.entity.Entities;
import com.elong.qadev.entity.Header;
import com.elong.qadev.entity.HttpCase;
import com.elong.qadev.entity.NameValuePair;
import com.elong.qadev.entity.RunAfter;
import com.elong.qadev.entity.RunBefore;
import com.elong.qadev.entity.UrlParam;

@Repository("httpCaseDao")
public class HttpCaseDaoImpl implements HttpCaseDaoI {
	Logger logger = Logger.getLogger(HttpCaseDaoImpl.class);
	
	@Autowired
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<Header> getHeaders(int caseId) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				Header.class);
		c.add(Restrictions.eq("caseId", caseId));
		return (ArrayList<Header>) c.list();
	}

	@Override
	public Entities getEntities(int caseId) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				Entities.class);
		c.add(Restrictions.eq("caseId", caseId));
		if(c.list().isEmpty()){
			return null;
		}
		Entities e = (Entities) c.list().get(0);
		return e;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<NameValuePair> getNameValuePairs(int caseId) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				NameValuePair.class);
		c.add(Restrictions.eq("caseId", caseId));
		return (ArrayList<NameValuePair>) c.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<UrlParam> getUrlParams(int caseId) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				UrlParam.class);
		c.add(Restrictions.eq("caseId", caseId));
		return (ArrayList<UrlParam>) c.list();
	}

	@Override
	public HttpCase getHttpCase(int caseId) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				HttpCase.class);
		c.add(Restrictions.eq("caseId", caseId));
		logger.debug("获取的结果是否为空："+c.list().isEmpty());
		if(c.list().isEmpty()){
			return null;
		}
		HttpCase h = (HttpCase) c.list().get(0);
		return h;
	}

	@Override
	public boolean saveHeaders(ArrayList<Header> ah) {
		Iterator<Header> it = ah.iterator();
		while(it.hasNext()){
			Header h = it.next();
			sessionFactory.getCurrentSession().save(h);
		}
		return true;
	}

	@Override
	public boolean saveEntities(Entities e) {
		sessionFactory.getCurrentSession().save(e);
		return true;
	}

	@Override
	public boolean saveNameValuePairs(ArrayList<NameValuePair> an) {
		Iterator<NameValuePair> it = an.iterator();
		while(it.hasNext()){
			NameValuePair h = it.next();
			sessionFactory.getCurrentSession().save(h);
		}
		return true;
	}

	@Override
	public boolean saveUrlParams(ArrayList<UrlParam> au) {
		Iterator<UrlParam> it = au.iterator();
		while(it.hasNext()){
			UrlParam h = it.next();
			sessionFactory.getCurrentSession().save(h);
		}
		return true;
	}

	@Override
	public boolean saveHttpCase(HttpCase hc) {
		sessionFactory.getCurrentSession().save(hc);
		return true;
	}

	@Override
	public boolean saveRunBefores(ArrayList<RunBefore> lb) {
		Iterator<RunBefore> it = lb.iterator();
		while(it.hasNext()){
			RunBefore h = it.next();
			sessionFactory.getCurrentSession().save(h);
		}
		return true;
	}

	@Override
	public boolean saveRunAfters(ArrayList<RunAfter> lr) {
		Iterator<RunAfter> it = lr.iterator();
		while(it.hasNext()){
			RunAfter h = it.next();
			sessionFactory.getCurrentSession().save(h);
		}
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<RunBefore> getRunBefores(int caseId) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				RunBefore.class);
		c.add(Restrictions.eq("caseId", caseId));
		return (ArrayList<RunBefore>) c.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<RunAfter> getRunAfters(int caseId) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				RunAfter.class);
		c.add(Restrictions.eq("caseId", caseId));
		return (ArrayList<RunAfter>) c.list();
	}

	@Override
	public boolean deleteHeaders(int caseId) {
		for(Header h : getHeaders(caseId)){
			sessionFactory.getCurrentSession().delete(h);
		}
		return true;
	}

	@Override
	public boolean deleteEntities(int caseId) {
		Entities e = getEntities(caseId);
		sessionFactory.getCurrentSession().delete(e);
		return true;
	}

	@Override
	public boolean deleteNameValuePairs(int caseId) {
		for(NameValuePair h : getNameValuePairs(caseId)){
			sessionFactory.getCurrentSession().delete(h);
		}
		return true;
	}

	@Override
	public boolean deleteUrlParams(int caseId) {
		for(UrlParam h : getUrlParams(caseId)){
			sessionFactory.getCurrentSession().delete(h);
		}
		return true;
	}

	@Override
	public boolean deleteHttpCase(int caseId) {
		HttpCase h = getHttpCase(caseId);
		sessionFactory.getCurrentSession().delete(h);
		return true;
	}

	@Override
	public boolean deleteRunBefores(int caseId) {
		for(RunBefore h : getRunBefores(caseId)){
			sessionFactory.getCurrentSession().delete(h);
		}
		return true;
	}

	@Override
	public boolean deleteRunAfters(int caseId) {
		for(RunAfter h : getRunAfters(caseId)){
			sessionFactory.getCurrentSession().delete(h);
		}
		return true;
	}

	@Override
	public boolean saveResponseAndResult(int caseId, boolean isExpected,
			String response) {
		logger.debug("开始执行存储dao...");
		HttpCase httpCase = getHttpCase(caseId);
		httpCase.setExpected(isExpected);
		httpCase.setResponse(response);
		sessionFactory.getCurrentSession().saveOrUpdate(httpCase);
		return true;
	}

	@Override
	public DBLink getDBLinkById(int sqlId) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				DBLink.class);
		c.add(Restrictions.eq("id", sqlId));
		logger.debug("获取的结果是否为空："+c.list().isEmpty());
		if(c.list().isEmpty()){
			return null;
		}
		DBLink h = (DBLink) c.list().get(0);
		return h;
	}


	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<DBLink> getDBLinks() {
		ArrayList<DBLink> dbLinks = (ArrayList<DBLink>) sessionFactory.getCurrentSession().createQuery("from DBLink").list();
		return dbLinks;
	}

	@Override
	public boolean saveDBLink(DBLink dbLink) {
		sessionFactory.getCurrentSession().save(dbLink);
		return true;
	}
	@Override
	public DBSql getDBSqlById(int sqlId) {
		Criteria c = sessionFactory.getCurrentSession().createCriteria(
				DBSql.class);
		c.add(Restrictions.eq("id", sqlId));
		logger.debug("获取的结果是否为空："+c.list().isEmpty());
		if(c.list().isEmpty()){
			return null;
		}
		DBSql h = (DBSql) c.list().get(0);
		return h;
	}
	@SuppressWarnings("unchecked")
	@Override
	public ArrayList<DBSql> getDBSqls() {
		ArrayList<DBSql> dbSqls = (ArrayList<DBSql>) sessionFactory.getCurrentSession().createQuery("from DBSql").list();
		return dbSqls;
	}

	@Override
	public boolean saveDBSql(DBSql dbSql) {
		sessionFactory.getCurrentSession().save(dbSql);
		return true;
	}

	@Override
	public boolean deleteDBLink(int id) {
		DBLink d = getDBLinkById(id);
		sessionFactory.getCurrentSession().delete(d);
		return true;
	}

	@Override
	public boolean deleteDBSql(int id) {
		DBSql s = getDBSqlById(id);
		sessionFactory.getCurrentSession().delete(s);
		return true;
	}

}
